package com.lun.swordtowardoffer2.c08;

import java.util.LinkedList;

import com.lun.util.BinaryTree.TreeNode;

public class BSTIterator {

	private LinkedList<TreeNode> stack;
	private TreeNode cur;

	public BSTIterator(TreeNode root) {
		this.cur = root;
		stack = new LinkedList<>();
	} 
	
	public int next() {
		while(cur != null) {
			stack.push(cur);
			cur = cur.left;
		}
		cur = stack.pop();
		int val = cur.val;
		cur = cur.right;
		return val;
	}
	
	public boolean hasNext() {
		return !stack.isEmpty() || cur != null;
	}
	
}
